<?php
/**
*   +----------------------------------------------------------------------
*   | Author: tb 
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-10-19
*   +----------------------------------------------------------------------
*   | Link (http://www.cloudsskill.com http://www.goshopx.com  http://www.shopingj.com )
*   +----------------------------------------------------------------------
**/

/**
 * 获取产品优惠券及信息类 (优惠券，巨划算、大宗批发、套餐批发)
 *
 * 查询信息默认的都是根据产品ID做索引
 */
class  Coupon {
	protected $coupon,$product,$usercoupon,$propackage;
	public function __construct(){
		$this->product = M( 'Product' );
		$this->coupon = M('ProductCoupon');
		$this->usercoupon = M("UserCoupon");
		$this->propackage = M('ProductPackage');
		
		
	}
	
	/*
	 * 获取产品优惠信息
	 * @date 2013-12-24
	 * @time 下午12:24
	 * @author tb
	 * @param $proids array or string or int or ''
	 * @param $type string ('SALE'、'USE'....)
	 * @param $field string ('id,product_id,value....')
	 * @return $res
	 * */
	function product_coupon($proids='' ,$type= 'SALE',$field=''){
		if($proids){
			$where['product_id'] = is_array($proids) ? array('IN',implode(',', $proids)) : $proids;
		}
		$where['type'] = is_array($type) ? array('IN',implode(',', $type)) : $type;
		if($field){
			$field .= ',starttime,endtime';
		}
		$res = array();	
		$res = $this->coupon->field($field)->where($where)->select();
		if(empty($res)) return $res;
		
		//嵌入过期情况
		foreach ($res as $key=>$vo){
			$res[$key]['enable'] = $this->checktime($vo['starttime'], $vo['endtime']);
		}
		return $res;
	}
	
	/*
	 * 获取产品套餐信息
	 * @date 2013-12-24
	 * @time 下午12:24
	 * @author tb
	 * @param proids array or string or int or ''
	 * @return $res
	 * */
	function product_package($proids='' ,$type= 'BIG',$field=''){
		if($type == 'BIG'){
			//大宗批发情况
			if($proids){
				$where['product_id'] = is_array($proids) ? array('IN',implode(',', $proids)) : $proids;
			}			
			$where['type'] = $type;
			if($field){
				$field .= ',start_time,end_time';
			}
			$res = array();
			$res = $this->propackage->field($field)->where($where)->select();$this->propackage->getLastSql();
			
			if(empty($res)) return $res;
			
			//嵌入过期情况
			foreach ($res as $key=>$vo){
				$res[$key]['enable'] = $this->checktime($vo['start_time'], $vo['end_time']);
			}
			return $res;
		}elseif($type== 'SERIES'){
			//组合套餐情况
		}
	}
	
	/**
	 *获取用户优惠券
	 *@date 2013-11-21
	 *@time 上午11；29
	 *@param $uid : 用户id
	 *@param $time : 是否要刷没过期的   1： 去掉过期优惠券，0 ：获取所有优惠券
	 */
	function user_coupon($uid,$time = 1){
		$map['userid'] = $uid;
		$map['status'] = 0;
		$res = $this->usercoupon->where($map)->select();
		
		if($time == 1){
			$list = array();
			foreach ($res as $key=>$vo){				
				$enable = $this->checktime($vo['starttime'], $vo['endtime']);					
				 if($enable){
				 	$list[$key] = $vo;
				 	$list[$key]['enable'] = $enable;
				 }						
			}
			return $list;
		}else{
			return $res;
		}
		
	}
	
	/**
	 *更改聚划算状态
	 *@date 2013-10-16
	 *@time 上午11；29
	 *@param $id : product_coupon表中的主键
	 *@param $status:更新的状态
	 */
	function ju_status($id , $status = 0){
		$model = M("ProductCoupon");
		$map['id'] = $id;
		$map['type'] = "SALE";
		$data['status'] = $status;
		$reid = $model->where($map)->save($data);
		return $reid;
	}
	
	/**
	 *判断某个时间是否过期
	 *@date 2013-9-14
	 *@author tb
	 *@param string $st 开始日期
	 *@param string $et 结束日期
	 *@return int $enable(0:已过期， 1:未过期)
	 */
	function checktime($st,$et){
		$time = time();
		if((!$st && $et && $et>=$time) || ($st && !$et && $st<=$time) || (!$st && !$et) ||($st && $et && $st<=$time && $et>=$time)){
			return 1;
		}else {
			return 0;
		}
	}
	
	/**
	 * 根据ID把该券更新为使用
	 * @param int $_sn
	 * @param int $_orderid
	 * @return boolean $reid
	 */
	function upuseconpon($_id,$_orderid){
		$model = M('UserCoupon');
		$data = array();
		$data['id'] = $_id; 
		$data['order_sn'] = $_orderid;
		$data['useip'] = ip2long(get_client_ip());
		$data['usetime'] = time();
		$data['status'] = 1;
		$reid = $model->save($data);
// 		dump($data);
// 		echo $model->getLastSql();
		return $reid;
	}
	
	/**
	 * 订单优惠券信息写入
	 * @param int $_orderid 订单ID
	 * @param int $_value 金额
	 * @param int $_uid
	 * @return boolean $reid
	 */
	function wordercoupon($_orderid,$_value,$_uid){
		$model = M("OrderCoupon");
		$data = array();
		$data['order_id'] = $_orderid;
		$data['subject'] = 'COUPON';
		$data['goods_id'] = "";
		$data['value'] = $_value;
		$data['userid'] = $_uid;
		$data['type'] = 'COUPON';
		$data['content'] = '优惠券使用';
		$reid = $model->add($data);
		return $reid;
	}
	
	/**
	 * 跟据ID检查该券是否使用,可以使用返回ID、面值、类型。
	 * @param int $_sn
	 * @return boolean $coupon
	 */
	function checkconpon($_sn){
		$model = M('UserCoupon');
		$where = array();
		$where['id'] = $_sn;
		$where['status'] = '0';
		$where['endtime'] = array('GT',time());
		$where['starttime'] = array('LT',time());
		$coupon = $model->field('id,type,value')->where($where)->find();
		return $coupon;
	}
	
	/**
	 * 更新优惠券状态
	 * @author tb
	 * @date 2013.12.27
	 * @param int $id 优惠券id
	 * @param int $status 更改的状态
	 * @return boolean $coupon
	 */
	function conpon_status($id,$status){
		$data = array();
		$data['id'] = $id;
		$data['status'] = $status;
		$reid = $this->usercoupon->save($data);
		return $reid;
	}
}

